//
//  FirstviewController.m
//  dongdongbook
//
//  Created by yaotd-PC on 2017/12/20.
//  Copyright © 2017年 yaotengdong. All rights reserved.
//

#import "FirstviewController.h"
#import "FirstCollectionViewCell.h"

@interface FirstviewController () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) UICollectionViewFlowLayout *laYout;
@property (nonatomic, retain) NSMutableArray *section0Array;


@end

@implementation FirstviewController

// 注意const的位置
static NSString *const cellId = @"cellId";
static NSString *const headerId = @"headerId";
static NSString *const footerId = @"footerId";

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blueColor];
    _section0Array = [NSMutableArray arrayWithObjects:@"asdsd", @"asdasd", @"aaaa", nil];
    
    self.laYout = [[UICollectionViewFlowLayout alloc] init];
    //    collectionView 的滚动方向
    _laYout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //    每个cell的大小
    _laYout.itemSize = CGSizeMake(100, 170);
    
    //    行间距(垂直于滚动方向的叫一行)
    _laYout.minimumLineSpacing = 20;
    //    最小列间距(每一行里面 item之间的间距)
    _laYout.minimumInteritemSpacing = 0;
    
    _laYout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);

    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:_laYout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [self.view addSubview:_collectionView];
//    注册cell、sectionHeader、sectionfooter
    [_collectionView registerClass:[FirstCollectionViewCell class] forCellWithReuseIdentifier:cellId];
//    [_collectionView registerNib:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId];
//    [_collectionView registerNib:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerId];
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    cell.myLabel.text = @"asd";
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [_activity startAnimating];
    [_begin setTitle:@"" forState:UIControlStateNormal];
    LSYReadPageViewController *pageView = [[LSYReadPageViewController alloc] init];
    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"mdjyml"withExtension:@"txt"];
    pageView.resourceURL = fileURL;    //文件位置
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        pageView.model = [LSYReadModel getLocalModelWithURL:fileURL];
        dispatch_async(dispatch_get_main_queue(), ^{
            [_activity stopAnimating];
            [_begin setTitle:@"Beign txt Read" forState:UIControlStateNormal];
            [self presentViewController:pageView animated:YES completion:nil];
//            下面的push会导致小说内容显示不全的布局问题
//            [self.navigationController pushViewController:pageView animated:YES];
        });
    });
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
